﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PeekWebApp.Management.AjaxHandler
{
    /// <summary>
    /// TourismTypeEditHandler 的摘要说明
    /// </summary>
    public class TourismTypeEditHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";            
            string action = context.Request["action"];
            if (action.Equals("Edit"))//编辑
            {
                string name = context.Request["name"].Trim();
                string remark = context.Request["remark"].Trim();
                string desc = context.Request["desc"];
                string code = context.Request["hidCode"];
                HttpPostedFile file = context.Request.Files["icon"];
                PeekService.TourismInfoType type = new PeekService.TourismInfoTypeService().GetModel(code);
                if (type == null)
                {
                    type = new PeekService.TourismInfoType();
                    type.Code = Guid.NewGuid().ToString();
                    type.CreateID = "";
                    type.CreateTime = DateTime.Now;
                    type.State = 0;
                    type.Sort = new PeekService.ExtendService().GetSort("TourismInfoType");
                    type.Des = desc;
                    type.Name = name;
                    type.Remark = remark;
                    if (file.ContentLength > 0)
                    {
                        Common.ImageUpload imgUpload = new Common.ImageUpload();
                        imgUpload.SavePath = "~/images/icon/";
                        imgUpload.Upload(file);
                        type.Icon = "/images/icon/" + imgUpload.OutFileName;
                    }
                    else
                    {
                        type.Icon = "";
                    }
                    new PeekService.TourismInfoTypeService().Add(type);
                }
                else
                {
                    type.Des = desc;
                    type.Name = name;
                    type.Remark = remark;
                    if (file.ContentLength > 0)
                    {
                        Common.ImageUpload imgUpload = new Common.ImageUpload();
                        imgUpload.SavePath = "~/images/icon/";
                        imgUpload.Upload(file);
                        type.Icon = "/images/icon/" + imgUpload.OutFileName;
                    }
                    else
                    {
                        type.Icon = "";
                    }
                    new PeekService.TourismInfoTypeService().Update(type);
                }
                context.Response.Redirect("../TourismInfoType/List.aspx");   
            }
            else //绑定
            {
                string code = context.Request["hidCode"];
                PeekService.TourismInfoType type = new PeekService.TourismInfoTypeService().GetModel(code);
                context.Response.Write(PeekService.CommonService.ConvertToJSON(type));
            }
                                           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}